package weibo.adapter;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;

import weibo.network.GetCommit;

/**
 * Created by Administrator on 2016/4/15.
 */
public class CommitAdapter extends BaseAdapter {

    GetCommit.ResponseGetComment responseGetComment = null;

    public void setResponseGetComment(GetCommit.ResponseGetComment responseGetComment) {
        this.responseGetComment = responseGetComment;
    }

    @Override
    public int getCount() {
        if (responseGetComment == null) return 0;
        return responseGetComment.getCommentList().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = View.inflate(MyApplication.share(), R.layout.item_weibo_commit,null);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.id_tv_time);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.id_tv_content);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        GetCommit.Comments comments = responseGetComment.getCommentList().get(position);
        viewHolder.tv_time.setText(comments.getComment_Time());
        StringBuffer sb = new StringBuffer();
        String name = comments.getUser_Name();
        String fuhao = "：";
        String content = comments.getComment_Content();
        sb.append(name).append(fuhao).append(content);

        SpannableStringBuilder style=new SpannableStringBuilder(sb.toString());
        style.setSpan(new ForegroundColorSpan(MyApplication.share().getResources().getColor(R.color.orange)),0,name.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(MyApplication.share().getResources().getColor(R.color.grey)),name.length(),name.length()+1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ForegroundColorSpan(MyApplication.share().getResources().getColor(R.color.black)),name.length()+1,sb.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        viewHolder.tv_content.setText(style);
        return convertView;
    }

    class ViewHolder
    {
        TextView tv_time;
        TextView tv_content;
    }
}
